// Copyright (c) 2010. Highly Intelligent Solutions.
// All rights reserved.
var HiS;
if(!HiS)
{
	HiS = {};
}
if(!HiS.Ui)
{
	HiS.Ui = {};
}
/*Constructor*/
HiS.Ui.AutoComplete = function(element, opts)
{
	this.init(element, opts);
};
/*Propiedades*/
HiS.Ui.AutoComplete.prototype.divResult=null;
HiS.Ui.AutoComplete.prototype.dataValue=null;
HiS.Ui.AutoComplete.prototype.dataText=null;
HiS.Ui.AutoComplete.prototype.selectedItem=null;
HiS.Ui.AutoComplete.prototype.selectedIdex=null;
HiS.Ui.AutoComplete.prototype.ajaxRequest=null;
HiS.Ui.AutoComplete.prototype.target=null;
HiS.Ui.AutoComplete.prototype.paramRequest=null;
HiS.Ui.AutoComplete.prototype.active=null;
/*Metodos*/
HiS.Ui.AutoComplete.prototype.init = function(target, opts)
{
	this.divResult = document.createElement('div');  	
  	this.divResult.style.display = "none"; 
	this.divResult.style.position = "absolute"; 
	this.divResult.className += (this.divResult.className ? " " : "") + "HisUi-mf-Corner-all HiSdivAutocomplete";	
	this.divResult.style.height = 300 + 'px';
	
	if(target)
	{		
		if(opts)
		{			
			if(opts.target)
			{
				var groupTarget = document.getElementById(opts.target);
				groupTarget.parentNode.appendChild(this.divResult);
				this.divResult.id =opts.target + "_HiStargetResponse";
				this.divResult.style.width = groupTarget.offsetWidth + 'px';
				this.target=[opts.target];
			}
			else
			{
				var groupTarget=document.getElementById(target[0]);
				groupTarget.parentNode.appendChild(this.divResult);						
				this.divResult.style.width = groupTarget.offsetWidth + 'px';
				this.divResult.id =target[0] + "_HiStargetResponse";				
				this.target=target;
			}
			this.paramRequest=target;
			for(i=0;i<target.length;i++)
			{	var HisAutoComplete=this;
				var targetInitHis = document.getElementById(target[i]);						
				this.addEventListener(targetInitHis, 'keypress', function(event)
				{ 
					var txt =(event.srcElement)?event.srcElement:this;
					var tecla=event.keyCode?event.keyCode:event.which;
					if(!(tecla== 38 || tecla== 40 || tecla== 37 || tecla== 39 || tecla== 17 || tecla== 18 ))
						if(txt.value.length>2)
						  HisAutoComplete.show();
				}, false);
				this.addEventListener(targetInitHis, 'keyup',function( event ) {
					
					if ( HisAutoComplete.active ) {
					switch(event.keyCode?event.keyCode:event.which) {				
					case 38://UP
						HisAutoComplete.activeItem(-1);						
						break;
					case 40://Down
						HisAutoComplete.activeItem(+1);						
						break;
					case 9:	
					case 13://Enter				
						
						var items=HisAutoComplete.divResult.getElementsByTagName("div");
						for(i=0;i<items.length ;i++)
						{
							if(items[i].id==HisAutoComplete.selectedIdex)		
							{
								//alert(items[i].Value);
								HisAutoComplete.selectedItem=eval(items[i].Value);
								HisAutoComplete.hide();
								if(HisAutoComplete.selectedItemChange!=null)
									HisAutoComplete.selectedItemChange();
								break;
							}
						}
						HisAutoComplete.selectedItem=eval(items[0].Value);
						HisAutoComplete.hide();
						if(HisAutoComplete.selectedItemChange!=null)
							HisAutoComplete.selectedItemChange();
						break;
					case 37:
					case 39:
					case 17:
					case 18://Ignore
						break;
					default:
						// ingresa otros digitos
						HisAutoComplete.activeItem(0);
						break;
					}
					}	
				}, false);
			
				this.addEventListener(targetInitHis, 'blur', function(e)
				{ 
					 setTimeout(function() {
						 HisAutoComplete.hide();
						}, 150 );
						
				}, false);
									
			}	
			
			if(opts.dataText)
			{
					this.dataValue=opts.dataValue;
					this.dataText=opts.dataText ;
			}
			if(opts.width)
			{
				this.divResult.style.width = opts.width  + 'px';
			}
			if(opts.height)
			{
				this.divResult.style.height = opts.height  + 'px';
			}
			if(opts.ajaxRequest)
			{
				this.ajaxRequest = opts.ajaxRequest ;
			}
		}		
		
	}	
	
};

HiS.Ui.AutoComplete.prototype.addEventListener = function(element, eventType, handler, capture)
{
	try
	{
		if (element.addEventListener)
		{
			element.addEventListener(eventType, handler, capture);
		}
		else if (element.attachEvent)
		{
			element.attachEvent('on' + eventType, handler);
		}
	}
	catch (e) {}
};

HiS.Ui.AutoComplete.prototype.show = function()
{
	if(this.ajaxRequest!=null)
	{
		var strRequest="";
		for(i=0;i<this.paramRequest.length;i++)
		{	
			var txt = document.getElementById(this.paramRequest[i]);
			strRequest+=(txt.name+ "=" +txt.value);
			if(i<this.paramRequest.length-1)
				strRequest+="&";
		}
		SubmitAjax(this.ajaxRequest+"?"+strRequest,this,'XML');
	}
	
	this.divResult.style.display = ""; 
};

HiS.Ui.AutoComplete.prototype.hide = function()
{
		this.divResult.style.display = "none"; 
		this.divResult.innerHTML="";	
};

HiS.Ui.AutoComplete.prototype.ajaxResponse = function(res)
{
	
	var xmlObj=res.documentElement;
	this.divResult.innerHTML="";
	this.active=false;
	if(xmlObj.childNodes.length==0)
		this.divResult.innerHTML="No existes Registros";
	else
		this.active=true;
	
	for(i=0;i< xmlObj.childNodes.length;i++)
	{
		var obj = xmlObj.childNodes[i];
		var div = document.createElement('div');  	  	
		if(i==this.selectedIdex)
			div.className = "HisUi-mf-Corner-all HiSdivAutocompleteItemActive";
		else			
			div.className =  "HisUi-mf-Corner-all HiSdivAutocompleteItem";
		div.id=i;
		div.Value="";
		
		if(obj.attributes.length)
		{			
			div.appendChild(document.createTextNode(obj.getAttribute(this.dataText)));		
			
			 for (var a=0; a<obj.attributes.length; a++)
			 {
				 var atributeName=(obj.attributes[a].nodeName).toString();
				 var atributeValue=(obj.attributes[a].nodeValue||"").toString();
				 div.Value+= ("'"+atributeName + "' : '"+ atributeValue+"'");
				 if(i< obj.attributes.length-1)
					 div.Value+=",";
			 }		
			 div.Value="selectedItem={" + div.Value + "};";			
		}
		else
		{	
			div.Value="selectedItem={ text:'" + obj.nodeValue + "' };";
			div.appendChild(document.createTextNode(obj.nodeValue));
		}
		
		this.divResult.appendChild(div);
		var HisAutoComplete=this;
		this.addEventListener(div, 'click', function(e)
				{	if(e.srcElement==null)
						HisAutoComplete.selectedItem=eval(this.Value);
					else
						HisAutoComplete.selectedItem=eval(e.srcElement.Value);
					HisAutoComplete.hide();
					if(HisAutoComplete.selectedItemChange!=null)
						HisAutoComplete.selectedItemChange();
				}, false);
	}	
};
HiS.Ui.AutoComplete.prototype.activeItem= function(index)
{
	this.selectedIdex =this.selectedIdex+index;	
	var items=this.divResult.getElementsByTagName("div");
	if(this.selectedIdex>=items.length-1)
		this.selectedIdex =items.length-1;
	else
		if(this.selectedIdex<0)
			this.selectedIdex=0;
	
	for(i=0;i<items.length ;i++)
	{
		if(items[i].id==this.selectedIdex)		
			items[i].className = (items[i].className ? " " : "") + "HisUi-mf-Corner-all HiSdivAutocompleteItemActive";
		else			
			items[i].className = (items[i].className ? " " : "") + "HisUi-mf-Corner-all HiSdivAutocompleteItem";
			
	}
};
HiS.Ui.AutoComplete.prototype.selectedItemChange =null;

